package org.lc.httpcollector.tool

import com.alibaba.fastjson.JSON
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass

/**
 * 在类中直接创建logger
 */
inline fun <reified T> T.logger(): Logger {
    return when {
        T::class.isCompanion -> LoggerFactory.getLogger(T::class.java.enclosingClass)
        this is Class<*> -> LoggerFactory.getLogger(this)
        this is KClass<*> -> LoggerFactory.getLogger(this.java)
        else -> LoggerFactory.getLogger(T::class.java)
    }
}

/**
 * 查找指定字符最后出现的位置
 */
fun String.lastIndexOfOrNull(char: Char, startIndex: Int = lastIndex, ignoreCase: Boolean = false) =
    lastIndexOf(char, startIndex, ignoreCase).let { if (it == -1) null else it }

fun String.replaceAll(items: Collection<String>, newStr: String): String =
    replace(Regex(items.joinToString("|") { Regex.escape(it) }), newStr)
